package com.mobiletrain.org.motherkitch;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class SplashActivity extends ActionBarActivity {


    private static final int GO_MAIN = 100;
    private static final int GO_GUIDE = 101;
    public static final String SHARED_NAME = "splash";
    private int TIME_DELAY = 3000;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case GO_MAIN:
                    Intent i = new Intent(SplashActivity.this, MainActivity.class);
                    SplashActivity.this.startActivity(i);
                    SplashActivity.this.finish();


                    break;
                case GO_GUIDE:
                    Intent ii = new Intent(SplashActivity.this, GuideActivity.class);
                    SplashActivity.this.startActivity(ii);
                    SplashActivity.this.finish();
                    break;
            }
        }
    };

    private void initGo() {
        SharedPreferences sharedPreferences =getSharedPreferences(SHARED_NAME,MODE_PRIVATE);
        boolean value = sharedPreferences.getBoolean(SHARED_NAME, true);
        if (value) {//第一次
            handler.sendEmptyMessageDelayed(GO_GUIDE, TIME_DELAY);
        } else {
            handler.sendEmptyMessageDelayed(GO_MAIN, TIME_DELAY);
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        initGo();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_splash, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
